/***************************************************************************
 *   Copyright (C) 2007 by Ivan Vasić                                      *
 *   ivasic@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "torrentdialog.h"
#include "globals.h"
#include "trackerdb.h"
#include "exceptions.h"
#include "exceptionhandler.h"

#include <QtGui>

TorrentDialog::TorrentDialog(QWidget* parent, Qt::WFlags fl)
		: QDialog(parent, fl)
{
	setupUi(this);

	m_completer = new QCompleter(this);
	QDirModel* dirModel = new QDirModel(m_completer);

	m_completer->setModel(dirModel);


	//QLineEdit* m_file;
	m_file->setCompleter(m_completer);
	m_completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);

	connect(btnBrowse, SIGNAL(clicked()), this, SLOT(onBrowse()));
	connect(btnOk, SIGNAL(clicked()), this, SLOT(registerTorrent()));
}


TorrentDialog::~TorrentDialog()
{}

void TorrentDialog::onBrowse()
{
	QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString::null, tr("Torrent files (*.torrent)"));

	if (fileName.isNull())
		return;

	m_file->setText(fileName);
}

void TorrentDialog::registerTorrent()
{
	if (m_name->text().isEmpty() || m_file->text().isEmpty())
	{
		QMessageBox::information(this, tr("QTracker"), tr("You must fill in all fields."));
		return;
	}

	QFile torFile(m_file->text());
	if (!torFile.exists())
	{
		QMessageBox::information(this, tr("QTracker"), tr("Selected torrent file does not exist. Please choose another file."));
		return;
	}

	torFile.open(QIODevice::ReadOnly);
	QByteArray metainfo = torFile.readAll();
	torFile.close();

	if (metainfo.length() == 0)
		return;

	TrackerDB* db = Globals::instance().db();

	try
	{
		db->registerTorrent(metainfo, m_name->text(), m_description->toPlainText());
	}		
	catch (DecodingException* ex)
	{
		ExceptionHandler::instance().protocolException(this->thread(), ex, TRUE);
		QMessageBox::critical(this, tr("QTracker"), tr("Metainfo file decoding error has occurred. Most likely your torrent file is corrupted. Check log file for more info."));

		return;
	}
	catch (TorrentExistsException* ex)
	{
		ExceptionHandler::instance().protocolException(this->thread(), ex, TRUE);
		QMessageBox::warning(this, tr("QTracker"), tr("A torrent with the same infohash already exists. Most likely you're trying to register a duplicate torrent file."));

		return;
	}
	catch (InvalidAnnounceURLException* ex)
	{
		ExceptionHandler::instance().protocolException(this->thread(), ex, TRUE);
		QMessageBox::warning(this, tr("QTracker"), ex->getMessage());

		return;
	}
	catch (UnknownUserException* ex)
	{
		ExceptionHandler::instance().protocolException(this->thread(), ex, TRUE);
		QMessageBox::warning(this, tr("QTracker"), ex->getMessage());

		return;
	}
	catch (DBException* ex)
	{
		ExceptionHandler::instance().protocolException(this->thread(), ex, TRUE);
		QMessageBox::critical(this, tr("QTracker"), tr("A database error occurred. Check out your log file for more information."));

		return;
	}
	catch (Exception* ex)
	{
		ExceptionHandler::instance().protocolException(this->thread(), ex, TRUE);
		QMessageBox::warning(this, tr("QTracker"), tr("An unknown error occured. Check out your log file for more information."));

		return;
	}

	QMessageBox::information(this, tr("QTracker"), tr("Torrent registered."));
	accept();
}
